from openai import OpenAI
import base64

client = OpenAI(
    base_url="http://127.0.0.1:9904/v1",
    api_key="token-abc123",
)

imagefile = "dog.jpg"
question = """
图中的狗是什么品种
"""

def chat_completion_test(question,imagefile,stream=True):
    with open(imagefile,"rb") as f:
      image_base64 = base64.b64encode(f.read()).decode("utf-8")
      
    content=[
       {'type':'text','text':question},
       {'type':'image_url','image_url':{"url": f"data:image/jpeg;base64,{image_base64}"}},
    ]
    messages = [
        {"role": "system", "content": "你是一个专业的AI助手"},
        {"role": "user", "content": content}
    ]


    response = client.chat.completions.create(
      model="Qwen2vl-7b",
      messages=messages,
      max_tokens=4096,
      stream=True, 
      temperature=0.7,
    )
    
    if stream:
      for chunk in response:
          if chunk_content := chunk.choices[0].delta.content:
            print(chunk_content, end="", flush=True)
    else:
      print(response.choices[0].message.content)
      
if __name__ == "__main__":
    chat_completion_test(question,imagefile,stream=True)

